// pages/photo/allResult/allResult.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    mb1:"none",
    mb2:"none",
    btnd:"block",
    mb3:"none",
    mb4:"none"

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'auth',
      success: function (res) {
        if (res['data'] == 1) {
          that.setData({
            btnd:"none"
          })
        } else {

        }
      },
    })
    wx.getStorage({
      key: 'path',
      success: function (res) {
        that.setData({
          path: res.data
        })
      },
    })
    wx.getStorage({
      key: 'cropPicsPath',
      success: function (res) {
        that.setData({
          cropPicsPath: res.data
        })
      },
    })
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })
    wx.getStorage({
      key: 'markPic',
      success: function (res) {
        that.setData({
          markPic:res.data
        })
      },
    })
    wx.getStorage({
      key: 'strData',
      success: function (res) {
        that.setData({
          strData: res.data
        })
      },
    })
    wx.getSystemInfo({

      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        
        let itemheight = clientWidth*0.94*0.15
        that.setData({
          itemheight:itemheight,
          height:clientHeight
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tosinResult:function(res){
    // console.log(res)
    var sinpicid = res.currentTarget.dataset.name
    // console.log(sinpicid)
    wx.request({
      url: base_url + '/sinpicDetail/',
      method: 'POST',
      data: {
        sinpicid: sinpicid
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          console.log(res.data['dataDic'])
          wx.setStorage({
            key: 'dataDic',
            data: res.data['dataDic'],
            success: function (res) {
              wx.navigateTo({
                url: '../sinResult/sinResult'
              })
            }
          })
        }
      }
    })
    
  },
  seePic:function(){
    wx.getStorage({
      key: 'path',
      success: function (res) {
        wx.previewImage({
          current: res.data, // 当前显示图片的http链接
          urls:[res.data]
        })
      },
    })
    
  },
  gotoT:function(){
    var that = this;
    // that.setData({
    //   mb1:"block"
    // })
    wx.showModal({
      title: "提示",
      content: "是否上传今日随堂练习,每日可上传一次",
      success: function (res) {
        if (res.confirm) {
          that.yesT()
        } else {
          
        }
      }
    })
  },
  noT:function(){
    var that = this;
    that.setData({
      mb1: "none",
      mb3:"none"
    })
  },
  //上传
  yesT:function(){
    var that = this
    wx.getStorage({
      key: 'student_id',
      success: function (res) {
        wx.request({
          url: base_url + '/uploadHomework/',
          method: 'POST',
          data: {
            student_id:res.data,
            path: that.data.path,
            cropPicsPath: that.data.cropPicsPath
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            if (res.data['flag'] == true) {
              // that.setData({
              //   mb1: "none",
              //   mb2: "block"
              // })
              // setTimeout(function () {
              //   that.setData({
              //     mb2: "none"
              //   })
              // }, 3000)
              wx.showModal({
                title: "提示",
                content: "上传成功",
                success: function (res) {
                  if (res.confirm) {

                  } else {

                  }
                }
              })
            } else if (res.data['flag'] == "-1"){
              // that.setData({
              //   mb1: "none",
              //   mb4: "block"
              // })
              // setTimeout(function () {
              //   that.setData({
              //     mb4: "none"
              //   })
              // }, 3000)
              wx.showModal({
                title: "提示",
                content: "您已经上传过，请勿重复上传",
                success: function (res) {
                  if (res.confirm) {

                  } else {

                  }
                }
              })
            } 
            else {
              // that.setData({
              //   mb1: "none",
              //   mb3: "block"
              // })
              // setTimeout(function () {
              //   that.setData({
              //     mb3: "none"
              //   })
              // }, 3000)
              wx.showModal({
                title: "提示",
                content: "请先加入班级",
                success: function (res) {
                  if (res.confirm) {
                    that.gotoAddclass()
                  } else {

                  }
                }
              })
            }
          }
        })
      },
    })
    



    
  },
  gotoAddclass: function () {
    wx.navigateTo({
      url: '../../my/visitor/addClass/addClass',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
        console.log(res)
      }
    })
  }
})